package com.neusoft.controller;

import com.neusoft.core.CommonResult;
import com.neusoft.core.Orders;
import com.neusoft.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/OrdersController")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/Orders")
    public CommonResult<Integer> createOrders(@RequestBody Orders orders) throws Exception{
        int result = ordersService.createOrders(orders);
        return new CommonResult<>(200,"success（10601）",result);
    }

    @GetMapping("/Orders")
    public CommonResult<Orders> getOrdersById(
            @RequestParam Integer orderId) throws Exception{
        Orders orders = ordersService.getOrdersById(orderId);
        return new CommonResult<>(200,"success(10601)",orders);
    }

    @RequestMapping("/OrdersList/{userId}")
    public CommonResult<List<Orders>> listOrdersByUserId(
            @PathVariable("userId") String userId) throws Exception{
        List<Orders> list = ordersService.listOrdersByUserId(userId);
        return new CommonResult(200,"success(10601)",list);
    }
    @PutMapping("/Orders")
    public CommonResult<List<String>> finishOrders(
            @RequestParam Integer orderId,
            @RequestParam(required = false, defaultValue = "1") Integer orderType,
            @RequestParam(required = false) Integer fromWalletId,
            @RequestParam(required = false) Integer toWalletId,
            @RequestParam(required = false) BigDecimal amount) throws Exception {
        
        List<String> result = ordersService.finishOrders(orderId, orderType, fromWalletId, toWalletId, amount);
        if (result != null && !result.isEmpty()) {
            return new CommonResult<>(400, "订单完成失败", result);
        }
        return new CommonResult<>(200, "success(10601)", null);
    }
}